/************************************************************************/
/*    Author: Scott Bevin									            */
/************************************************************************/

#include "PropertyContainer.h"
#include "PropertyFactory.h"
#include "..\XUtilities\Utilities.h"

namespace XF
{
	PropertyContainer::PropertyContainer()
	{
	}

	PropertyContainer::PropertyContainer(PropertyContainer &copy)
	{
		for(std::map<std::string, IProperty*>::const_iterator it = copy.mProperties.begin();
			it != copy.mProperties.end(); ++it)
		{
			IProperty* newProp = PropertyFactory::CreateProperty(it->second->TypeID(), it->second->Name(), it->second->ToString());
			AddProperty(newProp);
		}
	}

	PropertyContainer::~PropertyContainer()
	{
		RemoveAllProperties();
	}

	void PropertyContainer::AddProperty(IProperty *prop)
	{
		if(!HasProperty(prop->Name()))
		{
			mProperties[prop->Name()] = prop;
		}
	}

	IProperty *PropertyContainer::GetIProperty(const std::string &name)
	{
		std::map<std::string, IProperty*>::iterator it = mProperties.find(name);

		XF::Utilities::Assert(it != mProperties.end(), "Property does not exist: " + name);

		return it->second;
	}


	void PropertyContainer::RemoveProperty(const std::string &name)
	{
		std::map<std::string, IProperty*>::iterator it = mProperties.find(name);

		if(it != mProperties.end())
		{
			delete it->second;
			mProperties.erase(it);
		}
	}

	void PropertyContainer::RemoveAllProperties()
	{
		for(std::map<std::string, IProperty*>::iterator it = mProperties.begin();
			it != mProperties.end(); ++it)
		{
			delete it->second;
		}

		mProperties.clear();
	}
}
